package com.igd.socket.sdk.nio.multi_reactor;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class WorkHandler implements Runnable {
	
	private SocketChannel socketChannel;
	
	public WorkHandler(SocketChannel socketChannel) {
		this.socketChannel = socketChannel;
	}
 
	@Override
	public void run() {
		try {
			System.out.println("Server work---isConnected：" + socketChannel.isConnected() + " isOpen：" + socketChannel.isOpen() + " isRegistered：" + socketChannel.isRegistered()
								+ " finishConnect：" + socketChannel.finishConnect());
			ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
			socketChannel.read(byteBuffer);
			String reqMsg = new String(byteBuffer.array(), StandardCharsets.UTF_8);
			System.out.println("get req：" + reqMsg);
			String rspMsg = "resp：" + reqMsg;
			socketChannel.write(ByteBuffer.wrap(rspMsg.getBytes(StandardCharsets.UTF_8)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
